//
//  UINavigationBar+Extension.swift
//  AESOLAR_IOS
//
//  Created by Mr.Wang on 2022/8/15.
//  Copyright © 2022 Mr.Wang. All rights reserved.
//

import UIKit

extension UINavigationBar {
    
    func setBackgroundImage(_ image: UIImage?) {
        if #available(iOS 15.0, *) {
            let appearance = UINavigationBarAppearance()
            appearance.configureWithTransparentBackground()
            appearance.backgroundImage = image
            appearance.shadowImage = standardAppearance.shadowImage
            appearance.titleTextAttributes = standardAppearance.titleTextAttributes
            standardAppearance = appearance
            scrollEdgeAppearance = appearance
            compactAppearance = appearance
        } else {
            setBackgroundImage(image, for: .default)
        }
    }
    
    func setShadowImage(_ image: UIImage?) {
        if #available(iOS 15.0, *) {
            let appearance = UINavigationBarAppearance()
            appearance.configureWithTransparentBackground()
            appearance.backgroundImage = standardAppearance.backgroundImage
            appearance.shadowImage = image
            appearance.titleTextAttributes = standardAppearance.titleTextAttributes
            standardAppearance = appearance
            scrollEdgeAppearance = appearance
        } else {
            shadowImage = image
        }
    }
    
    func setTitleTextAttributes(_ attributes: [NSAttributedString.Key: Any]) {
        if #available(iOS 15.0, *) {
            let appearance = UINavigationBarAppearance()
            appearance.configureWithTransparentBackground()
            appearance.backgroundImage = standardAppearance.backgroundImage
            appearance.shadowImage = standardAppearance.shadowImage
            appearance.titleTextAttributes = attributes
            standardAppearance = appearance
            scrollEdgeAppearance = appearance
        } else {
            titleTextAttributes = attributes
        }
    }
    
}
